package core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap ;
import java.util.List;
import java.util.Map ;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.Service;
import common.Servlet;
import common.Validate;
import dict.BcDict;
import dict.CbDict;
import dict.CommonDict;
import dict.TransactionDict;
import dicts.GlobalDict;
import dicts.PaymentDict;
import entities.TBc;
import entities.TCb;
import interfaces.IBcService;
import interfaces.ICbService;
import net.sf.json.JSONObject;
import util.LogUtil;
import utils.DateUtil;
import utils.StringUtil;

public class Unity extends Servlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public synchronized void process(HttpServletRequest request, HttpServletResponse response) throws Exception {

		System.out.println(DateUtil.format(new Date(), DateUtil.DATETIME) + " | 资金对账");

		// 初始化service
		IBcService bcService = (IBcService) Service.create(IBcService.class);
		ICbService cbService = (ICbService) Service.create(ICbService.class);

		// 初始化bean和属性
		TBc bc = new TBc();
		TCb cb = new TCb();

		// 验证系统
		if (GlobalDict.LINUX.equalsIgnoreCase(System.getProperty("os.name")) && !CommonDict.HOST.equals(request.getServerName())) {
			response.getWriter().print(Validate.result(TransactionDict.ILLAGEL_ACCESS, TransactionDict.MESSAGES.get(TransactionDict.ILLAGEL_ACCESS)));
			return;
		}

		// 校验参数 ( 名称,值,校验模式,边界 )
		List<Object[]> parameterList = Collections.synchronizedList(new ArrayList<Object[]>());
		parameterList.add(new Object[] { "type", request.getParameter("type"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "bank", request.getParameter("bank"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "serial", request.getParameter("serial"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "amount", request.getParameter("amount"), Validate.NOTEMPTY | Validate.ISNUMBER | Validate.EXCEEDNUMBER, 0, Integer.MAX_VALUE });

		JSONObject validateResult = Validate.validate(parameterList);
		LogUtil.unity(Validate.summary(parameterList));

		if (!StringUtil.isEmpty(validateResult)) {
			System.out.println ( validateResult.toString() ) ;
			response.getWriter().print(validateResult.toString());
			return;
		}

		if (!request.getParameter("type").equals(PaymentDict.BC_BLUSH) && !request.getParameter("type").equals(PaymentDict.CB_REFUND)&& !request.getParameter("type").equals(PaymentDict.CB_REFUSE)) {
			response.getWriter().print(Validate.result(TransactionDict.UNITY_TYPE_ERROR, TransactionDict.MESSAGES.get(TransactionDict.UNITY_TYPE_ERROR)));
			return;
		}

		// 消费对账
		if (request.getParameter("type").equals(PaymentDict.BC_BLUSH)) {
			
			try {
				Map<String,Object> paramMap = Collections.synchronizedMap(new HashMap<String, Object>());
				paramMap.put("bcState", "1");
				paramMap.put("bcCstate", "0");
				paramMap.put("bcType", PaymentDict.BC_BLUSH);
				paramMap.put("bnkNo", request.getParameter("bank"));
				paramMap.put("bcCard", StringUtil.split(request.getParameter("serial")));
				paramMap.put("bcAmount", request.getParameter("amount"));
				bc=bcService.single ( paramMap );
			} catch (Exception e) {
				response.getWriter().print(Validate.result(BcDict.BC_FETCH_EXCEPTION, BcDict.MESSAGES.get(BcDict.BC_FETCH_EXCEPTION)));
				return;
			}

			if (bc == null) {
				response.getWriter().print(Validate.result(BcDict.BC_NOT_EXIST, BcDict.MESSAGES.get(BcDict.BC_NOT_EXIST)));
				return;
			}

			try {
				bc.setBcCstate("1");
				bc.setBcCdate(new Date());
				bcService.persist(bc);
			} catch (Exception e) {
				response.getWriter().print(Validate.result(BcDict.BC_PERSIST_EXCEPTION, BcDict.MESSAGES.get(BcDict.BC_PERSIST_EXCEPTION)));
				return;
			}

			response.getWriter().print(Validate.result(BcDict.BC_SUCCESS, BcDict.MESSAGES.get(BcDict.BC_SUCCESS)));
			return;

		}

		// 退款对账
		if (request.getParameter("type").equals(PaymentDict.CB_REFUND)) {

			try {
				Map<String,Object> paramMap = Collections.synchronizedMap(new HashMap<String, Object>());
				paramMap.put("cbState", "1");
				paramMap.put("cbCstate", "0");
				paramMap.put("cbType", PaymentDict.CB_REFUND);
				paramMap.put("bnkNo", request.getParameter("bank"));
				paramMap.put("cbCard", StringUtil.split(request.getParameter("serial")));
				paramMap.put("cbAmount", request.getParameter("amount"));
				cb=cbService.single ( paramMap );
			} catch (Exception e) {
				response.getWriter().print(Validate.result(CbDict.CB_FETCH_EXCEPTION, CbDict.MESSAGES.get(CbDict.CB_FETCH_EXCEPTION)));
				return;
			}
			if (cb == null) {
				response.getWriter().print(Validate.result(CbDict.CB_NOT_EXIST, CbDict.MESSAGES.get(CbDict.CB_NOT_EXIST)));
				return;
			}

			try {
				cb.setCbCstate("1");
				cb.setCbCdate(new Date());
				cbService.persist(cb);
			} catch (Exception e) {
				response.getWriter().print(Validate.result(CbDict.CB_PERSIST_EXCEPTION, CbDict.MESSAGES.get(CbDict.CB_PERSIST_EXCEPTION)));
				return;
			}

			response.getWriter().print(Validate.result(CbDict.CB_SUCCESS, CbDict.MESSAGES.get(CbDict.CB_SUCCESS)));
			return;
		}
		
		// 拒付对账
		if (request.getParameter("type").equals(PaymentDict.CB_REFUSE)) {

			try {
				Map<String,Object> paramMap = Collections.synchronizedMap(new HashMap<String, Object>());
				paramMap.put("cbState", "1");
				paramMap.put("cbCstate", "0");
				paramMap.put("cbType", PaymentDict.CB_REFUSE);
				paramMap.put("bnkNo", request.getParameter("bank"));
				paramMap.put("cbCard", StringUtil.split(request.getParameter("serial")));
				paramMap.put("cbAmount", request.getParameter("amount"));
				cb=cbService.single ( paramMap );
			} catch (Exception e) {
				response.getWriter().print(Validate.result(CbDict.CB_FETCH_EXCEPTION, CbDict.MESSAGES.get(CbDict.CB_FETCH_EXCEPTION)));
				return;
			}
			if (cb == null) {
				response.getWriter().print(Validate.result(CbDict.CB_NOT_EXIST, CbDict.MESSAGES.get(CbDict.CB_NOT_EXIST)));
				return;
			}

			try {
				cb.setCbCstate("1");
				cb.setCbCdate(new Date());
				cbService.persist(cb);
			} catch (Exception e) {
				response.getWriter().print(Validate.result(CbDict.CB_PERSIST_EXCEPTION, CbDict.MESSAGES.get(CbDict.CB_PERSIST_EXCEPTION)));
				return;
			}

			response.getWriter().print(Validate.result(CbDict.CB_SUCCESS, CbDict.MESSAGES.get(CbDict.CB_SUCCESS)));
			return;
		}
	}
}
